package com.sayi.dzapi.controller;


import org.springframework.core.io.FileSystemResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.dao.EmptyResultDataAccessException;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.Map;
import java.io.File;

@RestController
@RequestMapping("/smiley")
public class SmileyController {
    
    private static final Logger log = LoggerFactory.getLogger(UserService.class);
    
    @Autowired
    private JdbcTemplate jdbcTemplate;

    //TODO 直接返回表情
    @GetMapping("/{typeid:[0-9]+}_{code:[0-9]+}")
    public ResponseEntity handleSmileyRequest(@PathVariable int typeid,
                                      @PathVariable int code) {
        
        log.info("typeid: " + typeid + ",code: " + code);

        String sql="SELECT * FROM pre_forum_imagetype WHERE typeid = :typeid";
        MapSqlParameterSource parameters = new MapSqlParameterSource();
        parameters.addValue("typeid", typeid);

        String subSql="SELECT url FROM pre_common_smiley WHERE id = ?";
        try {
            Map<String, Object> imageType = new NamedParameterJdbcTemplate(jdbcTemplate).queryForMap(sql, parameters);
            log.info("imageType:"+imageType.toString());
            String dir=(String)imageType.get("directory");
            
            log.info("dir:" + dir);
            String url = jdbcTemplate.queryForObject(subSql, String.class, code);
            log.info("url:" + url);

            File imageFile=new File("/www/wwwroot/i.lty.fan/static/image/smiley/"+dir+"/"+url);

            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.IMAGE_PNG); // Adjust the MediaType based on the actual image type
            headers.setContentLength(imageFile.length());

            return new ResponseEntity<>(new FileSystemResource(imageFile),headers,HttpStatus.OK);
        } catch (EmptyResultDataAccessException e) {
            log.error(e.getMessage());
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An error occurred: " + e.getMessage());
        }
    }
    @GetMapping("/")
    public String hello(){
        return "hello";
    }
}